import 'dart:async';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:wch/utils/log_utils.dart';
import 'package:wch/widget/common_widget.dart';

import 'package:flutter_pdfview/flutter_pdfview.dart';

class ReportDetailPage extends StatefulWidget {
  const ReportDetailPage({super.key, required this.path});

  final String path;

  @override
  State<ReportDetailPage> createState() => _ReportDetailPageState();
}

class _ReportDetailPageState extends State<ReportDetailPage> {
  Uint8List? list;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: const Color(0xFFF8F9FB),
      child: list != null
          ? PDFView(
              pdfData: list!,
              enableSwipe: true,
              swipeHorizontal: false,
              autoSpacing: true,
              pageFling: false,
              onRender: (_pages) {},
              onError: (error) {
                LogUtils.log('onError=' + error.toString());
              },
              onPageError: (page, error) {
                LogUtils.log('$page: ${error.toString()}');
              },
              onViewCreated: (PDFViewController pdfViewController) {
                // _controller.complete(pdfViewController);
              },
            )
          : Container(),
    );
  }

  Uint8List loadPdfData(String assetName) {
    File file = File(assetName);
    return file.readAsBytesSync().buffer.asUint8List();
  }

  @override
  void initState() {
    super.initState();
    Timer.periodic(Duration(milliseconds: 500), (timer) {
      timer.cancel();
      setState(() {
        list = loadPdfData(widget.path);
      });
    });
  }

}
